package com.avic.modules.backup4Dameng.remote;

import ch.ethz.ssh2.*;
import com.avic.modules.backup4Dameng.DmpBackup;
import com.avic.utils.DateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;

@Component
public class ScpCopyExecute {


//    @Value("${spring.datasource.druid.database}")
//    private String schema;
//
//
//
//    public static void main(String[] args) throws IOException {
//
//        String addr = "192.168.2.253";
//        String logName = DateUtils.dateTimeNow().concat("dexp.log");
//        String dmpName = DateUtils.dateTimeNow().concat(".dmp");
//        String dexpPath = "D:\\\\software\\\\dm\\\\bin\\\\dexp";
//        String dexpURL = "HES/Admin123456@LOCALHOST:5555";
//        String exportDirectory = "C:\\\\Users\\\\Administrator\\\\Desktop\\\\hes\\\\sql";
//        String dexpCmd = String.format("%s %s DIRECTORY=%s FILE=%s SCHEMAS=HES TABLESPACE=N DROP=N LOG=%S  LOG_WRITE=N", dexpPath, dexpURL, exportDirectory, dmpName, logName);
//        String admin = "Administrator";
//        String adminPass = "Admin123";
//        System.out.println(dexpCmd);
//        Connection conn = new Connection(addr, 22);
//        String dmpFilePath = exportDirectory.concat("\\" + dmpName);
//        FileOutputStream fos = null;
//        try {
//            File dpFile = new File(DmpBackup.DMP_EXTERNAL_DIR.concat(dmpName));
//            fos = new FileOutputStream(dpFile);
//            conn.connect();
//            conn.authenticateWithPassword(admin, adminPass);
//            executeCommand(conn, dexpCmd);
//            SCPClient scpClient = conn.createSCPClient();
//            SCPInputStream scpInputStream = scpClient.get(dmpFilePath);
//            byte[] bytes = new byte[1024];
//            int len;
//            while ((len = scpInputStream.read(bytes)) != -1) {
//                fos.write(bytes, 0, len);
//            }
//            scpInputStream.close();
//            fos.close();
//            conn.close();
//        } catch (Exception ex) {
//            try {
//                if(fos!=null){
//                    fos.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            conn.close();
//            throw new RuntimeException("Dexp command exec wrong" + ex.getMessage());
//        }
//    }
//
//    public String exportDmp(String addr,String remoteDexpPath, String dexpURL, String remoteDexpDirectory,String winAdmin,String winAdminPass) {
//
//        String logName = DateUtils.dateTimeNow().concat("dexp.log");
//        String dmpName = DateUtils.dateTimeNow().concat(".dmp");
//        String dexpCmd = String.format("%s %s DIRECTORY=%s FILE=%s SCHEMAS=\"" + schema +"\" TABLESPACE=N DROP=N LOG=%S  LOG_WRITE=N", remoteDexpPath, dexpURL, remoteDexpDirectory, dmpName, logName);
//        Connection conn = new Connection(addr, 22);
//        String dmpFilePath = remoteDexpDirectory.concat("\\" + dmpName);
//        FileOutputStream fos = null;
//        try {
//            File dpFile = new File(DmpBackup.DMP_EXTERNAL_DIR.concat(dmpName));
//            fos = new FileOutputStream(dpFile);
//            conn.connect();
//            conn.authenticateWithPassword(winAdmin, winAdminPass);
//            executeCommand(conn, dexpCmd);
//            SCPClient scpClient = conn.createSCPClient();
//            SCPInputStream scpInputStream = scpClient.get(dmpFilePath);
//            byte[] bytes = new byte[1024];
//            int len;
//            while ((len = scpInputStream.read(bytes)) != -1) {
//                fos.write(bytes, 0, len);
//            }
//            scpInputStream.close();
//            fos.close();
//            conn.close();
//        } catch (Exception ex) {
//            try {
//                if(fos!=null){
//                    fos.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            conn.close();
//            throw new RuntimeException("Dexp command exec wrong" + ex.getMessage());
//        }
//        return dmpName;
//    }
//
//    private static void executeCommand(Connection conn, String cmd) throws IOException {
//        Session session;
//        session = conn.openSession();
//        session.execCommand(cmd);
//        pipePrint(session);
//    }
//
//    private static void pipePrint(Session session) throws IOException {
//        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout()), "gbk"));
//        String line;
//        while ((line = bufferedReader.readLine()) != null) {
//            System.out.println(line);
//        }
//    }

}
